package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.POJO.User;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Project ：learn_mybatis
 * @File ：MybatisDemo.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/3/23 11:18
 * @Description ：mybatis程序测试学习
 */

public class MybatisDemo {
    public static void main(String[] args) throws IOException {
        // 加载mybatis核心配置文件，获取SqlSessionFactory
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 获取SqlSession对象，用它来执行sql
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            List<User> users = sqlSession.selectList("test.selectAll");

            System.out.println(users);
        }
    }
}
